require 'xmpp4r'


class LoginController < ApplicationController

@@user = Hash.new

def index

end

def initialize
puts 'new login controller initialized'
end

def contact_tree
	#newl=LoginController::new.get_sess
	buddy_list='['
	contacts=session[:sess].get_buddies
	contacts.keys.each do |cjid|
	buddy_list+='#{cjid},'
	buddy_list=buddy_list.chop
	end
	buddy_list +=']'
	render_text "#{buddy_list}"
end


def sign_in
puts 'form submitted'
# Login
Jabber::debug=true
puts params['user_name']+'/ruby'
# Login
@jid = Jabber::JID::new(params['user_name']+'/ruby')
@jid.domain='oracle.com'
password = params['pwd']
#testing Login model creation
#new_user = Login.new(@jid,password)
#@@user[params['user_name']] = new_user
#session[:sess]= @log_in
#session[:sess].connect

puts('signed in to oracle')
end


def tab_view
puts 'form submitted'
# Login
#Jabber::debug=true
puts params['user_name']+'/ruby'
# Login
@jid = Jabber::JID::new(params['user_name']+'/ruby')
@jid.domain='oracle.com'
password = params['pwd']
#testing Login model creation
session[:sess]= Login.new(@jid,password)
#@@user[params['user_name']] = new_user
#session[:sess]= @log_in
#session[:sess].connect

puts('signed in to oracle')
end

def main
puts 'form submitted'
# Login
#Jabber::debug=true
puts params['user_name']+'/ruby'
# Login
@jid = Jabber::JID::new(params['user_name']+'/ruby')
@jid.domain='oracle.com'
password = params['pwd']
#testing Login model creation
session[:sess]= Login.new(@jid,password)
#@@user[params['user_name']] = new_user
#session[:sess].connect

puts('signed in to oracle')
end

def msg_handler
	
	#render_text "default template"
	content = ''
	#if request.xhr?
	#	rt="ajax request : #{@counter}"
	#	else
	#		rt="get/post request"
	#	end
	#newl = get_model
	p Thread.current
	    for m in session[:sess].throw
		content+=("<p><b>"+m.from.to_s().split('.')[0]+"</b> Says: "+m.body+"</p>")
		end
		render_text "#{content}"
end

def send_msg
session[:sess].create_message(params['newmessage'],nil)
render_text 'sent'
end



def contact_tree
	#newl=LoginController::new.get_sess
	buddy_list='['
	contacts=session[:sess].get_buddies
	contacts.keys.each do |cjid|
	buddy_list+="{\"text\":\"#{cjid}\",\"leaf\":true,\"cls\":\"file\"},"
	end
	buddy_list=buddy_list.chop
	buddy_list+=']'
	render_text "#{buddy_list}"
	end

def logout
#session[:sess].destroy
redirect_to :action => 'index' 
end

end   #end of the controller class
